/**
 * @copyright SilverBiology, LLC.
 * @author Michael Giddens
 * @website http://www.silverbiology.com
*/
Ext.ns('GBIF');

GBIF.NameGrid = function(config, id) {

	this.list = [];
	 
	this.language = new Ext.data.SimpleStore({
			fields: ['id', 'value']
		, data: [
					['1','GBIF Nub']
//				,	['6866','ITIS']
			]
	});
	
	this.dataset = new Ext.form.ComboBox({ 
			autoScroll: true
		,	lazyRender: true				
		,	mode:	'local'				
		,	autoWidth: true
		,	store: this.language
	 	, value: id
		,	displayField:'value'
		,	valueField:'id'
		,	typeAhead:false
		,	triggerAction: 'all'
		,	editable:false
		,	width:76
		,	listeners: {
				render: function(){
					this.setValue('1');
				}
			}  
	});

	this.btnClassify  = new Ext.Button({
			text: 'Classify'
		,	scope: this
		,	handler: this.classify
	});

	this.store = new Ext.data.JsonStore({
			proxy: new Ext.data.HttpProxy({ url: "resources/api/get.php", timeout: 60000 })
		,	root: 'data'
		,	idProperty: 'taxonID'
		,	baseParams: {
					cmd: 'taxagrid'
				,	rkey: ''
			}
		,	fields: [
					{name: 'taxonID'}
				,	{name: 'scientificName'}
				,	{name: 'genus'}
				,	{name: 'family'}
				,	{name: 'class'}
				,	{name: 'order'}
				,	{name: 'phylum'}
				,	{name: 'kingdom'}
			]
	});

	this.taxonGrid = new Ext.grid.GridPanel({
			store: this.store
		,	columns: [
					{header: "ID", width: 50, sortable: true, dataIndex: 'taxonID'}
				,	{header: "Scientific Name", width: 240, sortable: true, dataIndex: 'scientificName'}
				,	{header: "Genus", width: 180, sortable: true, dataIndex: 'genus'}
				,	{header: "Family", width: 95, sortable: true, dataIndex: 'family'}
				,	{header: "Class", width: 80, sortable: true, dataIndex: 'class'}
				,	{header: "Order", width: 85, sortable: true, dataIndex: 'order'}
				,	{header: "Phylum", width: 70, sortable: true, dataIndex: 'phylum'}
				,	{header: "Kingdom", width: 65, sortable: true, dataIndex: 'kingdom'}
			]
		,	view: new Ext.grid.GridView({
					forceFit: false
				,	emptyText: 'No information available.'
				,	deferEmptyText: false
			})
		,	tbar:['Source: ', this.dataset, '&nbsp;&nbsp;', this.btnClassify]
		,	sm: new Ext.grid.RowSelectionModel({singleSelect:true})
		,	border: false
		,	loadMask: false
		,	autoScroll: true
		,	bbar: new Ext.ux.StatusBar({ 
					statusAlign: 'right'
				,	defaultText: 'Select source from list and click "Classify" to begin.'					
//				,	items: [{xtype: 'tbtext', text: ''}] 
			})
		,	listeners: {
				rowdblclick:function(grid, rowIndex, e){
					var id = grid.getStore().getAt(rowIndex).get('taxonID');
					window.open('http://names.gbif.org/usage/'+ id);
				}
			}
	});

	this.exportButton = new Ext.ux.Exporter.Button({
			component: this.taxonGrid
		,	hidden: true
		,	cellBorderColor: '#000000'
		,	title: 'TaxonTagger Export'
		,	text: 'Download Excel File'
	});
	
	this.taxonGrid.getBottomToolbar().add( this.exportButton );
				
	Ext.apply(this,config,{
			title: 'Classification Viewer'
		,	width: 900
		,	height: 500
		,	layout: 'fit'
		,	items: [ this.taxonGrid ]
	});

	GBIF.NameGrid.superclass.constructor.call(this,config);
};

//---------------------------------------------

Ext.extend(GBIF.NameGrid, Ext.Window, {					 									

	 	classify: function(){
			this.btnClassify.disable();
			this.exportButton.hide();
			this.store.removeAll();
	 		this.store.baseParams.rkey = this.dataset.getValue();
		  this.list = this.originalList.slice();
			this.loadRow();
			this.taxonGrid.getBottomToolbar().showBusy();
		}
	 
	,	loadRows: function(list){
		  this.originalList = list;
	 	}
	 
	,	loadRow: function() {
			if (!Ext.isEmpty(this.list)){
				name = this.list.shift();
				var listSubArray = [];
				var ls = '[{"name": ' + '"'+ name + '"'+ '}]';
				listSubArray.push(ls);
				this.taxonGrid.getBottomToolbar().setText('Loading ' + (this.originalList.length - this.list.length ) + ' of ' + this.originalList.length + ' Names');
				this.store.load({
						params: {
							list: ls
						}
					,	add: true
					,	scope: this
					,	callback: this.loadRow
				});
			} else {
				this.taxonGrid.getBottomToolbar().clearStatus();
				this.taxonGrid.getBottomToolbar().setText('Double click row to view source website.');
				this.exportButton.show();
				this.btnClassify.enable();			
			}
		}
});